﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace Qetza.TeamFoundation.WorkItemQuery.Admin
{
    /// <summary>
    /// Command to rename a work item query.
    /// </summary>
    public class CommandRenameWiq : ICommand
    {
        #region ICommand Members

        /// <inheritdoc />
        public string Name
        {
            get { return "rename"; }
        }

        /// <inheritdoc />
        public string HelpText
        {
            get { return ResourceStrings.RenameWiqHelpText; }
        }

        /// <inheritdoc />
        public IEnumerable<ParameterInfo> AllowedParameters
        {
            get
            {
                return new[] {
                   new ParameterInfo() { 
                        Name = CoreArgumentNames.Server, 
                        ArgumentName = ResourceStrings.ServerArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ServerHelpText
                        },
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Project, 
                        ArgumentName = ResourceStrings.ProjectArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ProjectHelpText
                        },
                    new ParameterInfo() {
                        Name = CoreArgumentNames.QueryPath,
                        ArgumentName = ResourceStrings.QueryNameArgument,
                        IsRequired = true,
                        HelpText = ResourceStrings.Rename_QueryNameHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.New,
                        ArgumentName = ResourceStrings.QueryNewNameArgument,
                        IsRequired = true,
                        HelpText = ResourceStrings.Rename_QueryNewNameHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.NoPrompt,
                        IsFlag = true,
                        HelpText = ResourceStrings.NoPromptHelpText
                        }
                    };
            }
        }

        /// <inheritdoc />
        public void Execute(IDictionary<string, string> parameters)
        {
            if (parameters.ContainsKey(CoreArgumentNames.NoPrompt) || Utility.Confirm(string.Format(ResourceStrings.RenameWiqConfirm, parameters[CoreArgumentNames.QueryPath], parameters[CoreArgumentNames.New])))
            {
                // get query
                var query = TfsUtility
                    .GetWorkItemStore(parameters[CoreArgumentNames.Server], parameters.ContainsKey(CoreArgumentNames.NoPrompt))
                    .Projects[parameters[CoreArgumentNames.Project]]
                    .StoredQueries
                    .Find(parameters[CoreArgumentNames.QueryPath]);

                if (query != null)
                {
                    // rename query
                    query.Name = parameters[CoreArgumentNames.New];
                    query.Update();

                    Console.WriteLine(ResourceStrings.RenameWiqCompleted);
                }
            }
        }

        /// <inheritdoc />
        public void ValidateParameters(IDictionary<string, string> parameters)
        {
            return;
        }

        #endregion
    }
}
